﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataBaseLibrary.DBInterfaces;
using DataBaseLibrary.DBObjectsFB;

namespace DocMreoLiteVersion
{
    /// <summary>
    /// Interaction logic for SearchResultTable.xaml
    /// </summary>
    public partial class SearchResultTable : Window
    {

        public object ReturnObject { get; private set; }


        private Type type;

        public SearchResultTable()
        {
            InitializeComponent();
        }

        public SearchResultTable(IDBLogic.ResultFields fields)
            : this()
        {


            if (fields.ValueFields == null || fields.ValueFields.Count == 0)
            {
                MessageBox.Show("Ниодной записи не найдено!");
                return;
            }

            //Создаем столбцы
            for (int i = 0; i <  fields.NamesFields.Length; i++)
            {
                MessageBox.Show(fields.ValueFields[0][i].GetType().ToString());
                if (fields.ValueFields[0][i].GetType() == typeof(DateTime))
                    DocDataGrid.Columns.Add(new DataGridTextColumn() { Header = fields.NamesFields[i], Binding = new Binding(String.Format("[{0}]", i)) { StringFormat = "d", ConverterCulture = CultureInfo.CreateSpecificCulture("ru-RU") } });
                else
                    DocDataGrid.Columns.Add(new DataGridTextColumn() { Header = fields.NamesFields[i], Binding = new Binding(String.Format("[{0}]",i))});
            }
           
            DocDataGrid.ItemsSource = fields.ValueFields;
        }



        private void DocDataGrid_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                ReturnObject = (DocDataGrid.SelectedItem as object[])[0];
                DialogResult = true;
            }
            catch (SystemException err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            ReturnObject = (DocDataGrid.SelectedItem as object[])[0];
            DialogResult = true;
        }

        private void CloseClick(object sender, RoutedEventArgs e)
        {
            ReturnObject = null;
            DialogResult = false;
        }


    }
}
